﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class listAbuseReportsOutput : Api_BaseOutput
	{
		public listAbuseReportsResults result { get; set; }
		public listAbuseReportsParms inputParms { get; private set; }
		// --------------------------------------------------------
		public listAbuseReportsOutput( listAbuseReportsInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new listAbuseReportsResults();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "Abuse Reports: {0}", result.total );
		}

		#region ================================================================== format setup
		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}
		// ---- xmlrpc
		internal void Format( x_listAbuseReports rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}
		#endregion

		#region ================================================================= Output
		// ---------------------------------------------------------- outputXML
		protected void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			DateTime dt; int it;
			try
			{
				int.TryParse( xml.Element( "total" ).Value, out it );
				result.total = it;
				//
				IEnumerable<XElement> data = xml.Element( "data" ).Elements( "struct" );
				foreach ( XElement entry in data )
				{
					listAbuseReportsResults.listAbuseReport report = new listAbuseReportsResults.listAbuseReport();
					DateTime.TryParse( entry.Element( "date" ).Value, out dt );
					report.date = dt;
					report.email = entry.Element( "email" ).Value ?? "";
					report.campaign_id = entry.Element( "campaign_id" ).Value ?? "";
					report.type = entry.Element( "type" ).Value ?? "";
					this.result.data.Add( report );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- outputJSON
		protected void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_listAbuseReports jsonResult = jss.Deserialize<x_listAbuseReports>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------
		private listAbuseReportsResults FormatResults( x_listAbuseReports x_results )
		{
			listAbuseReportsResults results = new listAbuseReportsResults();
			results.total = x_results.total;
			//
			DateTime dt;
			foreach ( var item in x_results.data )
			{
				listAbuseReportsResults.listAbuseReport report = new listAbuseReportsResults.listAbuseReport();
				DateTime.TryParse( item.date, out dt );
				report.date = dt;
				report.email = item.email ?? "";
				report.campaign_id = item.campaign_id ?? "";
				report.type = item.type ?? "";
				//
				results.data.Add( report );
			}
			return results;
		}
		#endregion
	}
}
